#!/usr/bin/perl -w
# 
# @param $1 install home
# @param $2 delimiter
# @param $3 host
# @param $4 port
# @param $5 username
# @param $6 password
#
# -- 05/25/2009 Created by Zhixin Li
# -- executed in slave host to check report_host and report_port setting 
# status   0  normal
#          1  report_host wrong
#          2  not set  wrong
#          3  report_port wrong
#          4  both report_host and report_port are wrong 
#
use strict;

my ($MYSQL_HOME,$DELIM,$HOST,$PORT,$USER,$PASS) = @ARGV;
my $PARAM="";
$PARAM="$PARAM -h\"$HOST\"" if (defined($HOST) && $HOST ne "");
$PARAM="$PARAM -P\"$PORT\"" if (defined($PORT) && $PORT ne "");
$PARAM="$PARAM -u\"$USER\"" if (defined($USER) && $USER ne "");
$PARAM="$PARAM -p\"$PASS\"" if (defined($PASS) && $PASS ne "");
my $MYSQL="$MYSQL_HOME/bin/mysql";
my $MYSQLADMIN="$MYSQL_HOME/bin/mysqladmin";
if ($^O =~ m/MSWin32/) {
  $MYSQL =~ s/\//\\/g;
  $MYSQLADMIN =~ s/\//\\/g;
}
# Check if mysqld is alive
my $retmsg=`$MYSQLADMIN $PARAM ping 2>&1`;
chomp($retmsg);
if ( "$retmsg" ne "mysqld is alive" ) {
    print "em_error=Down${DELIM}Down${DELIM}Down\n";
    exit 1;
}

#  get hostip
my $name = `hostname`;
chomp($name);
my $ipinfo = `grep $name /etc/hosts|grep -v "#"`;
my @con = split (/[ \t]+/, $ipinfo);
my $hostip = $con[0];
chomp($hostip);
my $SQL1="show variables like 'pid_file'";
my $SQL2="show variables like 'port'";
my $m_file=`$MYSQL -N -s $PARAM --batch -e "$SQL1"`;
my $m_port=`$MYSQL -N -s $PARAM --batch -e "$SQL2"`;
my $pidfile = substr($m_file,index($m_file,"\t")+1,length($m_file)-index($m_file,"\t")+1);
my $myport = substr($m_port,index($m_port,"\t")+1,length($m_port)-index($m_port,"\t")+1);
my $ostype = `uname`;
my $mypid;
my $proc;
chomp($pidfile);
chomp($myport);
open (FILE, "$pidfile") or die "Couldn't open $pidfile for reading: $!\n";    # get mysqld pid
$/=""; 
$mypid = <FILE>; 
close(FILE); 
$/="\n";
chomp($mypid);
chomp($ostype);
my $x;
my $y;
my @z;
my @fields;
my $val1 = 0;
my $val2 = 0;
my $rephost = '0.0.0.0';
my $repport = '0000';
my @info;
my $mydef;

if ($ostype eq 'SunOS') {
   $proc =`/usr/ucb/ps awwx|/bin/grep -v grep|/bin/grep mysqld|/bin/grep -w "$mypid "`; 
   $x = substr($proc,index($proc,"/"),length($proc));              # get process command
   $y = grep(/--defaults-file/,$x);                                # check if mysqld start with --defaults-file option
} else {
   $proc =`/bin/ps awwx|/bin/grep -v grep|/bin/grep mysqld|/bin/grep -w "$mypid "`;
   $x = substr($proc,index($proc,"/"),length($proc));
   $y = grep(/--defaults-file=/,$x);
}

if ( $y == 1) {
   @z = split (/ +/, $x);
   foreach (@z) {
      if (/^--defaults-file=/) {
         $mydef = `$MYSQL_HOME/bin/mysqld $_ --print-defaults|/bin/grep -v arguments`;
      } 
   }
} else {
   $mydef = `$MYSQL_HOME/bin/mysqld --print-defaults|/bin/grep -v arguments`;
}
##   below version 5.1, print-defaults output with report-host, and version 5.1 output with report_host
my $count1 = grep(/--report.*host=/,$mydef);
my $count2 = grep(/--report.*port=/,$mydef);

if ($count1 == 0 && $count2 == 0) {
    my $repstatus = '2';
    print "em_result=${rephost}${DELIM}${repport}${DELIM}${repstatus}\n";
    exit 0;
} 

if ($count1 == 0) {
    $rephost = '0.0.0.0';
    $val1 = 1;
} 

if ($count1 == 1) {
    @info = split (/ +/, $mydef);
    foreach (@info) {
       if (/^--report.*host=/) {
          chomp($_);
          @fields = split(/=/,$_);
          $rephost = $fields[1];
          if ("$rephost" ne "$hostip") {
             $val1 = 1;
          }
       }
    }
} 

if ($count2 == 0) {
    $val2 = 3;
}

if ($count2 == 1) {
    @info = split (/ +/, $mydef);
    foreach (@info) {
        if (/^--report.*port=/) {
           chomp($_);
           @fields = split(/=/,$_);
           $repport = $fields[1];
           if ("$repport" ne "$myport") {
              $val2 = 3;
           }
        }
    }
}

my $repstatus = $val1 + $val2;
print "em_result=${rephost}${DELIM}$repport${DELIM}${repstatus}\n";
exit 0;
